# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="2"

inherit eutils cmake-utils subversion flag-o-matic confutils

DESCRIPTION="Multiprotocol Qt4-based instant messenger"
HOMEPAGE="http://www.qutim.org"
ESVN_REPO_URI="http://qutim.org/svn/qutim/trunk"
TRURI="http://qutim.org/svn/languages/trunk"
SRC_URI=""
LICENSE="GPL-2"

SLOT="live"
KEYWORDS=""
IUSE="+icq +jabber mrim irc vkontakte msn floaties imagepub nowlistening +plugman coder chess
	protocolicon +histman weather yandexnarod connectioncheck nowplaying accountname urlpreview
	twitter kdeintegration massmessaging sqlhistory youtubedownload libnotify webhistory debug"

# Костыль. FIXME!
LANGS_DIRLIST="bg_BG cs_CZ de_DE ru uk_UA"
TRLIST=""

# Используемые языки
LANGS="bg cs de ru uk"

i=0
for X in ${LANGS} ; do
	i=$[i+1]
	if ( use linguas_${X} ) ; then
		TRLIST="${TRLIST} `echo ${LANGS_DIRLIST} | gawk '{ print $'$i' }'`"
	fi
 	IUSE="${IUSE} linguas_${X}"
done

# Зависимости runtime
RDEPEND=">=x11-libs/qt-gui-4.5
	>=x11-libs/qt-webkit-4.5"

# Общие зависимости
DEPEND="${RDEPEND}
	>=dev-util/cmake-2.6
	!net-im/qutim:0.2"

# Постзависимости
PDEPEND="icq? ( x11-plugins/qutim-icq:live )
        jabber? ( x11-plugins/qutim-jabber:live )
        mrim? ( x11-plugins/qutim-mrim:live )
	irc? ( x11-plugins/qutim-irc:live )
	vkontakte? ( x11-plugins/qutim-vkontakte:live )
	twitter? ( x11-plugins/qutim-twitter:live )
	msn? ( x11-plugins/qutim-msn:live )
	plugman? ( x11-plugins/qutim-plugman:live )
	histman? ( x11-plugins/qutim-histman:live )
	yandexnarod? ( x11-plugins/qutim-yandexnarod:live )
	floaties? ( x11-plugins/qutim-floaties )
	imagepub? ( x11-plugins/qutim-imagepub )
	nowlistening? ( x11-plugins/qutim-nowlistening )
	protocolicon? ( x11-plugins/qutim-protocolicon )
	weather? ( x11-plugins/qutim-weather )
	connectioncheck? ( x11-plugins/qutim-connectioncheck )
	accountname? ( x11-plugins/qutim-accountname )
	nowplaying? ( x11-plugins/qutim-nowplaying )
	urlpreview? ( x11-plugins/qutim-urlpreview:live )
	coder? ( x11-plugins/qutim-coder )
	chess? ( x11-plugins/qutim-chess )
	kdeintegration? ( x11-plugins/qutim-kdeintegration:live )
	massmessaging? ( x11-plugins/qutim-massmessaging )
	sqlhistory? ( x11-plugins/qutim-sqlhistory:live )
	youtubedownload? ( x11-plugins/qutim-youtubedownload )
	libnotify? ( x11-plugins/qutim-libnotify:live )
	webhistory? ( x11-plugins/qutim-webhistory:live )"

# Если собираем с дебагом выключаем strip бинарников
RESTRICT="mirror
	debug? ( strip )"

pkg_setup() 
{
	# Проверки правильности задания USE флагов
	confutils_require_any icq jabber mrim irc vkontakte twitter
	confutils_use_conflict nowlistening nowplaying
}

src_unpack()
{
	# Распаковка исходников
 	subversion_src_unpack

	cd ${S}

	# Распаковка переводов
	mkdir languages
	cd languages
	for LANG in ${TRLIST} ; do
		einfo "Getting ${LANG} translation sources"
		svn co ${TRURI}/${LANG}/sources ${LANG}
		einfo
	done
}

src_prepare()
{
        # Перенос файла версий (FIX для последних вариантов CMakeList.txt)
        mkdir .svn
        cp -f ${ESVN_STORE_DIR}/${ESVN_PROJECT}/${ESVN_REPO_URI##*/}/.svn/entries .svn/	

	# Настройка параметров сбоки
	CMAKE_IN_SOURCE_BUILD=1
	if (use debug) ; then
	unset CFLAGS CXXFLAGS
		append-flags -O1 -g -ggdb
		CMAKE_BUILD_TYPE="Debug"
	fi
	mycmakeargs="-DUNIX=1 -DBSD=0 -DAPPLE=0 -DMINGW=0 -DWIN32=0 -DPhonon=0 -DCMAKE_INSTALL_PREFIX=/usr"

	sed -i "/plugin_path +=/s/lib/$(get_libdir)/" ${S}/src/pluginsystem.cpp
}

src_install()
{
	# Компиляция переводов
        einfo "Compiling translates"
        LRELEASE="/usr/bin/lrelease"
        for LANG in ${TRLIST} ; do
                einfo "Compiling ${LANG} translation"
                for FILE in `ls languages/${LANG}/*.ts` ; do
                        QM="languages/${LANG}/`basename ${FILE} | sed 's/ts$/qm/'`"
                        $LRELEASE ${FILE} -qm ${QM}
                        rm -f ${FILE}
                done
		rm -fR languages/${LANG}/.svn/
                einfo
        done

	cmake-utils_src_install

	dodir /usr/share/${PN}
	mv ${S}/languages ${D}/usr/share/${PN} || die "Failed to install locales"

        doicon icons/${PN}_64.png || die "Failed to install icon"
}

pkg_postinst()
{
	einfo
	einfo "Many plugins is unstable. If you have problems with qutim try to unmerge unnecessary plugins."
	einfo
}
